package com.ww.core.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.ww.core.R;
import com.ww.core.util.StringUtils;

public class InputDialog {
	Context context;
	EditText content;
	Dialog dlg;
	int inputType=-1;

	public InputDialog(Context context) {
		this.context = context;
	}

	public void show(String title, View.OnClickListener okOnClickListener) {
		show(title, null,null, okOnClickListener);
	}
	
	public void setType(int type) {
		inputType=type;
	}

	public void show(String title, String tip, View.OnClickListener okOnClickListener) {
		show(title, tip,null, okOnClickListener);
		}
	
	public void show(String title, String tip,String txt, View.OnClickListener okOnClickListener) {
		dlg = new Dialog(context, R.style.MyDialog);
		dlg.show();
		Window window = dlg.getWindow();
		window.setContentView(R.layout.core_dialog_input);
		content = (EditText) window.findViewById(R.id.content);
		TextView title_txt = (TextView) window.findViewById(R.id.title);
		if (StringUtils.isNotEmpty(title)) {
			title_txt.setText(title);
		}
		
		if(inputType!=-1){
			content.setInputType(inputType);
		}
		
		if (StringUtils.isNotEmpty(txt)) {
			content.setText(txt);
		}

		TextView tip_txt = (TextView) window.findViewById(R.id.tip);
		if (StringUtils.isNotEmpty(tip)) {
			tip_txt.setText(tip);
			tip_txt.setVisibility(View.VISIBLE);
		}

		Button ok = (Button) window.findViewById(R.id.ok);
		ok.setOnClickListener(okOnClickListener);
		Button cancel = (Button) window.findViewById(R.id.cancel);
		cancel.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				dlg.cancel();
			}
		});
	}

	public void cancel() {
		if (dlg != null) {
			dlg.cancel();
		}
	}

	public String getText() {
		return content.getText().toString();
	}

}
